/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.rs.domain.constants.RecordTemplateConstants;
import com.xy.biz.rs.domain.req.*;
import com.xy.tool.context.UserContextHolder;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.rs.domain.resp.GetRecordTemplateResponse;
import com.xy.biz.rs.domain.query.RecordTemplateQuery;
import com.xy.biz.rs.domain.converter.RecordTemplateConverter;
import com.xy.biz.rs.client.RecordTemplateClient;
import com.xy.biz.rs.service.RecordTemplateService;

/**
 *
 *
 * @author wwj
 * @date 2021-4-9
 */
@Api(tags = "记录模板")
@Slf4j
@RestController
@RequestMapping("/api/rs/record_template")
public class RecordTemplateController implements RecordTemplateClient {

	@Autowired
	private RecordTemplateService recordTemplateService;

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveRecordTemplate(@RequestBody SaveRecordTemplateRequest request) {
		return recordTemplateService.saveRecordTemplate(request);
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllRecordTemplate(@RequestBody Collection<SaveRecordTemplateRequest> collection) {
		recordTemplateService.saveAllRecordTemplate(RecordTemplateConverter.toBO(collection));
		return Result.success();
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateRecordTemplate(@RequestBody UpdateRecordTemplateRequest request) {
		return recordTemplateService.updateRecordTemplate(request);
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update_status")
	public Result<?> updateRecordTemplateStatus(@RequestBody UpdateRecordTemplateStatusRequest request) {
		return recordTemplateService.updateRecordStatusTemplate(RecordTemplateConverter.toBO(request));
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/appoint_updater")
	public Result<?> appointUpdater(@RequestBody AppointUpdaterRequest request) {
		return recordTemplateService.appointUpdater(request);
	}

	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteRecordTemplate(@RequestBody GetRecordTemplateRequest request) {
		recordTemplateService.deleteRecordTemplate(request.getRecordTemplateId());
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetRecordTemplateResponse> getRecordTemplate(@RequestBody GetRecordTemplateRequest request) {
		return recordTemplateService.getRecordTemplate(request.getRecordTemplateId());
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetRecordTemplateResponse>> listRecordTemplateById(@RequestBody Collection<Long> collection) {
		return Result.success(RecordTemplateConverter.toResponse(recordTemplateService.listRecordTemplateById(collection)));
	}

	@PostMapping(value = "/list")
	@Override
	public Result<List<GetRecordTemplateResponse>> listRecordTemplate(@RequestBody ListRecordTemplateRequest request) {
		return Result.success(RecordTemplateConverter.toResponse(recordTemplateService.listRecordTemplate(RecordTemplateConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countRecordTemplate(@RequestBody ListRecordTemplateRequest request) {
		return Result.success(recordTemplateService.countRecordTemplate(RecordTemplateConverter.toQuery(request)));
	}

	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetRecordTemplateResponse>> pageRecordTemplate(@RequestBody ListRecordTemplateRequest request) {
		RecordTemplateQuery query = RecordTemplateConverter.toQuery(request);
		List<GetRecordTemplateResponse> list = RecordTemplateConverter.toResponse(recordTemplateService.listRecordTemplate(query));
		int count = recordTemplateService.countRecordTemplate(query);
		return Result.success(new Page<>(list, count));
	}

	@PostMapping(value = "/page_update_request")
	public Result<Page<GetRecordTemplateResponse>> pageUpdateRequest(@RequestBody ListRecordTemplateRequest request) {
		RecordTemplateQuery query = RecordTemplateConverter.toQuery(request);
		query.setStatus(RecordTemplateConstants.TEMPLATE_STATUS_NOT_MOTIFY);
		query.setUpdateUserId(UserContextHolder.getUserId());
		List<GetRecordTemplateResponse> list = RecordTemplateConverter.toResponse(recordTemplateService.pageUpdateRequest(query));
		int count = recordTemplateService.countRecordTemplate(query);
		return Result.success(new Page<>(list, count));
	}

    @PostMapping(value = "/page_print_list")
    public Result<Page<GetRecordTemplateResponse>> pagePrintList(@RequestBody ListRecordTemplateRequest request) {
        RecordTemplateQuery query = RecordTemplateConverter.toQuery(request);
        List<GetRecordTemplateResponse> list = RecordTemplateConverter.toResponse(recordTemplateService.pagePrintList(query));
        return Result.success(Page.getPage(list,request.getPageIndex(),request.getPageSize()));
    }


}
